package com.hundsun.yr.universal.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.hundsun.yr.universal.library.R;


/**
 * @Title: HSSquareLinearLayout.java 
 * @Description: 正方形的 LinearLayout
 * @date 2015-12-31 下午3:07:35 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
public class HSSquareLinearLayout extends LinearLayout {
	
	/**长宽比**/
	private float ratio=1.0f;
	
    public HSSquareLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public HSSquareLinearLayout(Context context, AttributeSet attrs) {
    	this(context, attrs, 0);
    }

    public HSSquareLinearLayout(Context context) {
        this(context,null);
        
    }
    
    private void init(Context context, AttributeSet attrs){
    	
    	TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HSSquareLinearLayout);
    	
    	ratio=a.getFloat(R.styleable.HSSquareLinearLayout_hs_sl_ratio, 1.0f);
    	a.recycle();
    }
    

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY);
        heightMeasureSpec =MeasureSpec.makeMeasureSpec((int)(getMeasuredWidth()*ratio), MeasureSpec.EXACTLY);
        
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
